package com.example.fakeflappybird;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private FakeGameView gameView;
    int realboardscore=0;
    int bestboardscore=0;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        findViewById(R.id.hypermuteki).setBackgroundColor(Color.TRANSPARENT);
        findViewById(R.id.muteki).setBackgroundColor(Color.TRANSPARENT);
        findViewById(R.id.restartbtn).setBackgroundColor(Color.TRANSPARENT);
        findViewById(R.id.pause).setBackgroundColor(Color.TRANSPARENT);
        findViewById(R.id.standingby).setBackgroundColor(Color.TRANSPARENT);
        FakeData fakeData=new FakeData(this,"ffb.db",null,3);
        SQLiteDatabase db=fakeData.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from fakeflappybird",null);
        if(!cursor.moveToFirst()){
            db.execSQL("insert into fakeflappybird (bestscore) values(0)");
        }
        cursor=db.rawQuery("select * from fakeflappybird",null);
        cursor.moveToFirst();
        if(cursor!=null)
        bestboardscore=cursor.getInt(0);
        gameView=findViewById(R.id.RealGameView);
        gameView.setGameListener(new GameListener() {
            @Override
            public void addscore(int score) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        realboardscore=score;
                        ((TextView)findViewById(R.id.upscore)).setText(""+score);
                    }
                });

            }

            @Override
            public void gameover() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((TextView)findViewById(R.id.boardscore)).setText(""+realboardscore);
                        if(realboardscore>bestboardscore)
                        {
                            ContentValues values=new ContentValues();
                            values.put("bestscore",realboardscore);
                            db.update("fakeflappybird",values,"bestscore=?",new String[]{String.valueOf(bestboardscore)});
                            bestboardscore=realboardscore;
                        }
                        ((TextView)findViewById(R.id.boardbestscore)).setText(""+bestboardscore);
                        ((RelativeLayout)findViewById(R.id.boardcontainer)).setVisibility(View.VISIBLE);
                    }
                });
            }

            @Override
            public void renewButtonHyperMuteki() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        findViewById(R.id.hypermuteki).setVisibility(View.VISIBLE);
                        findViewById(R.id.hypermuteki).setEnabled(true);
                    }
                });
            }

            @Override
            public void renewButtonMuteki() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        findViewById(R.id.muteki).setVisibility(View.VISIBLE);
                        findViewById(R.id.muteki).setEnabled(true);
                    }
                });
            }
        });
        findViewById(R.id.restartbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        findViewById(R.id.boardcontainer).setVisibility(View.GONE);
                        ((TextView)findViewById(R.id.upscore)).setText(""+0);
                        realboardscore=0;
                        gameView.gamerestart();
                    }
                });
            }
        });
        findViewById(R.id.hypermuteki).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        gameView.mutekibird();
                        findViewById(R.id.hypermuteki).setEnabled(false);
                        findViewById(R.id.hypermuteki).setVisibility(View.GONE);
                    }
                });
            }
        });
        findViewById(R.id.muteki).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        gameView.minibird();
                        findViewById(R.id.muteki).setEnabled(false);
                        findViewById(R.id.muteki).setVisibility(View.GONE);
                    }
                });
            }
        });
        findViewById(R.id.pause).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    gameView.gamepause();
                    }
                });
            }
        });
        findViewById(R.id.standingby).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        gameView.gamestandingby();
                    }
                });
            }
        });
    }
}
